package com.qingke.hibernate;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.qingke.hibernate.bean.Student;

public class ProjectionDemo {
	public static void main(String[] args){
		try{
			SessionFactory sf=new Configuration().configure().buildSessionFactory();
			Session session=sf.openSession();
			
			DetachedCriteria detachCriteria=DetachedCriteria.forClass(Student.class);
			Criteria criteria=detachCriteria.getExecutableCriteria(session);
			
			ProjectionList projList=Projections.projectionList();
			projList.add(Projections.countDistinct("id"));
			projList.add(Projections.min("accommodation"));
			projList.add(Projections.groupProperty("gender"));
			
			
			criteria.setProjection(projList);
			
			
			List<?>  students=criteria.list();
			for(Object o:students){
				System.out.println(Arrays.toString((Object[])o));
			}
		
		}catch(HibernateException e){
			e.printStackTrace();
		}
		
	}
}
